﻿
 
//Login Page scripts start
// -------------------------------------
var prevSelection = "tabLogin";
var selectedItemsList = "";
var prevHomePageSelection = "tabServices";
var totalPrice = 0;
function HandleEventsLoginPage() {
 
    //Login/Register clicked navigation
    $("#navbar ul li").bind("click", function () {

        var newSelection = $(this).children("a").attr("data-tab-class");
        $("." + prevSelection).addClass("ui-screen-hidden");
        $("." + newSelection).removeClass("ui-screen-hidden");
        prevSelection = newSelection;
    });

    //Login button, make ajax call to login.
    $("#loginButton").bind("click", function () {
        var userName = $("input#txtLoginUserName").val();
        var password = $("#txtLoginPassword").val();
        var validateUrl = "http://192.168.0.100/restservice/RestServiceImpl.svc/json/" + userName; //sample get request

        $.ajax({
            cache: false,
            type: "GET",
            url: validateUrl,
            //contentType: 'application/json',
            dataType: "json",
            success: function (data) {
                if (data == true) {
                    // alert("Login Success");
                }
                //else; //alert("Login Failure");

            },
            error: function (xhr) {
                //alert("Login Failure");
            }
        });

        $.mobile.changePage("HomePage.html", { transition: "pop" }); //Todo move this to Login Success block
    });
}
//Login Page scripts End ------------------------------------------------

//Home Page Scripts Start     -----------------------------------------------
function HandleEventsHomePage() {
    appendToServicesList();

    $("#navbar ul li").live("click", function () {
        var newSelection = $(this).children("a").attr("data-tab-class");
        $("." + prevHomePageSelection).addClass("ui-screen-hidden");
        $("." + newSelection).removeClass("ui-screen-hidden");
        prevHomePageSelection = newSelection;

        if (newSelection == "tabHistory") {
            $("#itemHistoryListView").empty();
            $("#itemHistoryListView").append(itemsListView).listview("refresh"); //todo get the actrual
        }
        else if (newSelection == "tabServices") {
            $("#servicesList").empty();
            appendToServicesList();
        }

    });

}

function appendToServicesList() {
    $("#servicesList").append(servicesListView).listview("refresh");
}

var servicesListView =  function GetAllServicesList(  projectId) {  
// todo Need API to Get from server.
var htmlString = "<li><a href=\"OrdersPage.html\">Adigas</a></li>";
htmlString += "<li><a href=\"OrdersPage.html\">Kamath</a></li>";
return htmlString; 
};
//Home Page Scripts End     -----------------------------------------------


//Order Page scripts Starts----------

function HandleEventsOrderPage() {
    appendToItemsList();

    $('#itemListView li a').live("click", function (event) {
        $(this).simpledialog({
            'mode': 'string',
            'prompt': 'Enter the quantity',
            'cleanOnClose': true,
            'buttons': {
                'OK': {
                    click: function () {

                        if (parseInt($(this).attr('data-string')) != NaN) //todo not working
                            $($(this).children()[2]).text($(this).attr('data-string'));  // todo find out proper selector instead of using children
                    }
                },
                'Cancel': {
                    click: function () { console.log(this); },
                    icon: "delete",
                    theme: "c"
                }
            }
        })
    });


    $("#orderButton").bind("click", function () {
        selectedItemsList = "";
        totalPrice = 0;
        $(".ui-li-count").each(function () {
            if ($(this).html() != 0) {
                var costSpan = $($(this).parent().children()[1]).text(); // This will return ex : "₹ 40" 

                var cost = costSpan.split(" ");  //Split by space and get the actual amount
                var cost = costSpan.split(" ");  //Split by space and get the actual amount in cost[1]

                totalPrice = totalPrice + ( parseInt(cost[1]) * parseInt($(this).html()) );
                selectedItemsList += "<li>" + $(this).parent().parent().html() + "</li>";
            }
        });
        $.mobile.changePage("ReviewOrderPage.html", { transition: "slideup" });
    });
}

function appendToItemsList() {
    $("#itemListView").append(itemsListView).listview("refresh");
}

var itemsListView =  function GetAllServicesList(  projectId) {
    // todo Need API to Get from server. For images get base64 string from server <img src="..." />
    var htmlString = "<li><a >  <img src=\"images.jpg\" />   Rava Idli  <h3>₹ <span>40</span></h3> <span class=\"ui-li-count\">0</span> </a></li>";
    htmlString += "<li><a >  <img src=\"rava_idli.jpg\" />   Rava Idli  <h3>₹ <span>40</span></h3> <span class=\"ui-li-count\">1</span> </a></li>";
    htmlString += "<li><a >  <img src=\"images.jpg\" />   Rava Idli     <h3>₹ <span>40</span></h3> <span class=\"ui-li-count\">0</span> </a></li>";
 
return htmlString;
};



//Order Page scripts Ends ----------


//Review Page

function HandleEventsReviewOrderPage() {

    appendToReviewItemsList();

    }

    function appendToReviewItemsList() {
        $("#totalPriceDisplay").html(totalPrice);
    $("#orderedItemListView").append(selectedItemsList).listview("refresh");
}



//Revie Page end